package com.example.tianling.service.Impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.tianling.common.vo.TrainListVo;
import com.example.tianling.dao.TrainResultMapper;
import com.example.tianling.entity.BookEntity;
import com.example.tianling.entity.RoomEntity;
import com.example.tianling.entity.TrainResultEntity;
import com.example.tianling.service.TrainResultService;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;

@Service
public class TrainResultServiceImpl extends ServiceImpl<TrainResultMapper, TrainResultEntity> implements TrainResultService {

    @Autowired
    private TrainResultMapper trainResultMapper;

    @Override
    public List<TrainListVo> getTrainList(Integer userId) {
        MPJLambdaWrapper<Object> wrapper = new MPJLambdaWrapper<>()
                .select(TrainResultEntity::getRoomId)
                .select(RoomEntity::getType, RoomEntity::getRealNumber, RoomEntity::getStartTime)
                .select(BookEntity::getBookName)
                .eq(TrainResultEntity::getUserId, userId)
                .orderByDesc(RoomEntity::getStartTime)
                .leftJoin(RoomEntity.class, RoomEntity::getRoomId, TrainResultEntity::getRoomId)
                .leftJoin(BookEntity.class, BookEntity::getBookId, RoomEntity::getBookId);
        List<TrainListVo> trainList = trainResultMapper.selectJoinList(TrainListVo.class, wrapper);
        return trainList;
    }

    @Override
    public List<TrainResultEntity> getAllData(Integer roomId) {
        List<TrainResultEntity> trainResultList = this.lambdaQuery()
                .select(TrainResultEntity::getUserId, TrainResultEntity::getPersonalTime, TrainResultEntity::getDiscussionNum, TrainResultEntity::getSummaryTime)
                .eq(TrainResultEntity::getRoomId, roomId)
                .list();
        return trainResultList;
    }

    @Override
    public TrainResultEntity getMyResult(Integer roomId, Integer userId) {
        TrainResultEntity trainResult = this.lambdaQuery()
                .select(TrainResultEntity::getUserId, TrainResultEntity::getPersonal, TrainResultEntity::getPersonalAi, TrainResultEntity::getNote,
                        TrainResultEntity::getBookList, TrainResultEntity::getStartTime, TrainResultEntity::getStartType, TrainResultEntity::getLocalPath)
                .eq(TrainResultEntity::getRoomId, roomId)
                .eq(TrainResultEntity::getUserId, userId)
                .one();
        return trainResult;
    }

    @Override
    public void updateNote(Integer roomId, Integer userId, String note) {
        this.lambdaUpdate()
                .set(TrainResultEntity::getNote, note)
                .eq(TrainResultEntity::getRoomId, roomId)
                .eq(TrainResultEntity::getUserId, userId)
                .update();
    }

    @Override
    public void updateLocalPath(Integer roomId, Integer userId, String localPath) {
        this.lambdaUpdate()
                .set(TrainResultEntity::getLocalPath, localPath)
                .eq(TrainResultEntity::getRoomId, roomId)
                .eq(TrainResultEntity::getUserId, userId)
                .update();
    }
}
